//
//  LoginController.m
//  village_ios
//
//  Created by mac on 2019/8/11.
//  Copyright © 2019 com.sw.village. All rights reserved.
//

#import "LoginController.h"
#import "RegisterController.h"
@interface LoginController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;



@end

@implementation LoginController

//从导航
+ (void)presentNaviLoginController:(id)sourceVC loginSuccess:(LoginSuccess)loginSuccess {
    
    LoginController *vc = [LoginController instanceWithStoryBoard];
    vc.loginSuccess = loginSuccess;
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:vc];
    [sourceVC presentViewController:navi animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (IBAction)toLogin:(id)sender {
    
    //校验
    if (self.passwordTextField.text == nil ||
        self.phoneTextField.text == nil) {
        [SYProgressHUD showInfoText:@"信息不能为空"];
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (self.phoneTextField.text) {
        [params addEntriesFromDictionary:@{@"username":self.phoneTextField.text}];
    }
    if (self.passwordTextField.text) {
        [params addEntriesFromDictionary:@{@"password":self.passwordTextField.text}];
    }
    [SYProgressHUD show];
    [APIManager requestWithUrlString:@"api/auth/login" params:params method:@"POST" success:^(id response) {
        [SYProgressHUD showSuccessText:@"登录成功"];

        if(self.loginSuccess){
            self.loginSuccess();
        }
    } failure:^(NSError *error) {
        [SYProgressHUD showInfoText:@"登录失败"];
    }];
    
}
- (IBAction)toRegister:(id)sender {
    RegisterController *vc = [RegisterController instanceWithStoryBoard];
    [self.navigationController pushViewController:vc animated:YES];
    
}
@end
